package com.example.medical.controller;

import com.example.medical.service.EquipmentImageService;
import com.example.medical.domain.EquipmentImage;
import com.example.medical.util.AjaxResuit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@Api(tags = "器械图片管理（0525王婕）")
@RequestMapping("/api/equipmentImage")
public class EquipmentImageController {

    private final EquipmentImageService equipmentImageService;
    public EquipmentImageController(EquipmentImageService equipmentImageService) {
        this.equipmentImageService = equipmentImageService;
    }
    // 器械图片上传接口，接收器械图片相关信息及图片文件，调用Service层方法进行图片上传操作并返回相应结果
    @ApiOperation("器械图片上传")
    @PostMapping("/upload")
    public AjaxResuit uploadEquipmentImage(@ModelAttribute EquipmentImage equipmentImage, @RequestPart("image") MultipartFile imageFile) {
        boolean result = equipmentImageService.uploadEquipmentImage(equipmentImage, imageFile);
        if (result) {
            return AjaxResuit.success("器械图片上传成功");
        } else {
            return AjaxResuit.error("器械图片上传失败");
        }
    }
    // 删除器械图片接口，根据传入的器械图片ID调用Service层方法删除对应图片，并返回相应结果
    @ApiOperation("删除器械图片")
    @DeleteMapping("/deleteImage")
    public AjaxResuit deleteEquipmentImage(int id) {
        boolean result = equipmentImageService.deleteEquipmentImage(id);
        if (result) {
            return AjaxResuit.success("器械图片删除成功");
        } else {
            return AjaxResuit.error("器械图片删除失败");
        }
    }
    // 更新器械图片信息接口（比如图片标题、描述等信息更新），接收更新后的器械图片对象，调用Service层方法进行更新操作并返回相应结果
    @ApiOperation("更新器械图片信息")
    @PutMapping("/updateImage")
    public AjaxResuit updateEquipmentImage(@ModelAttribute EquipmentImage equipmentImage) {
        boolean result = equipmentImageService.updateEquipmentImage(equipmentImage);
        if (result) {
            return AjaxResuit.success("器械图片信息更新成功");
        } else {
            return AjaxResuit.error("器械图片信息更新失败");
        }
    }
    @ApiOperation("根据器械名称查询器械图片信息")
    @GetMapping("/getImagesByEquipmentName")
    public AjaxResuit getImagesByEquipmentName(String equipmentName) {
        List<EquipmentImage> imageList = equipmentImageService.getImagesByEquipmentName(equipmentName);
        if (imageList!= null &&!imageList.isEmpty()) {
            return AjaxResuit.success("查询成功", imageList);
        } else {
            return AjaxResuit.error("未找到对应器械的图片信息");
        }
    }
    // 获取所有器械图片列表接口，调用Service层方法获取所有器械图片列表并返回相应结果
    @ApiOperation("获取所有器械图片列表")
    @GetMapping("/getAllImages")
    public AjaxResuit getAllImages() {
        List<EquipmentImage> imageList = equipmentImageService.getAllImages();
        if (imageList!= null &&!imageList.isEmpty()) {
            return AjaxResuit.success("查询成功", imageList);
        } else {
            return AjaxResuit.error("暂无器械图片信息");
        }
    }
}